<html>

<head>
    <meta charset="UTF-8">
    <title>服务编排表格服务调用信息</title>
    <link id="easyuiTheme"  href="../../static/easyui/themes/bootstrap/easyui.css" rel="stylesheet" type="text/css"/>
    <link href="../../static/easyui/themes/icon.css" rel="stylesheet" type="text/css"/>
    <link href="../../static/css/esaas-easyui-add.css" rel="stylesheet" type="text/css"/>
    <script src="../../static/easyui/jquery.min.js" type="text/javascript"></script>
    <script src="../../static/easyui/jquery.cookie.js" type="text/javascript"></script>
    <script src="../../static/easyui/ui-theme.js" type="text/javascript"></script>
    <script src="../../static/easyui/jquery.easyui.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../static/easyui/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
    <script src="../../static/plugin/assets/jslib.util.js" type="text/javascript"></script>
    <script src="../../static/plugin/assets/jslib.jquery.ajax.js" type="text/javascript"></script>
    <script src="../../static/plugin/assets/jslib.easyui.util.js" type="text/javascript"></script>
    <script src="../../ffres/dict/all.js" type="text/javascript"></script>
    <a href="" target="_blank" id="commonLink" name="commonLink"></a>
    <style type="text/css">
        .hidden {
            display: none;
        }
    </style>
</head>

<body class="easyui-layout" scroll="yes">
    <div data-options="region:'center',split:false,collapsible:false,fit:true" style="width:100%;height:90%">
        <table id="dgIn" class="easyui-datagrid" title="" style="width:100%;height:90%"
            data-options="singleSelect:true,collapsible:true,method:'post', onSelect: onInSelect,onClickCell: onClickCell,onEndEdit:onEndEdit"
            title="Load Data" rownumbers="true" pagination="true" pageSize="20">
            <thead>
                <tr>
                    <th data-options="field:'recordKeyid',resizable:true" hidden="true">记录唯一编号</th>
                    <th data-options="field:'nodeId',resizable:true" hidden="true">节点标识</th>
                    <th data-options="field:'inFieldCode',resizable:true" >输入参数代码</th>
                    <th data-options="field:'inFieldName',resizable:true" >输入参数名称</th>
                    <th data-options="field:'evalSource',resizable:true,editor:{
                        type:'combobox',
                        options:{
                            valueField:'enname',
                            textField:'cnname',
                            data:EVAL_SOURCE
                        }
                    }" formatter="formatter_EVAL_SOURCE">赋值来源</th>
                    <th width="20%" data-options="field:'evalContent',resizable:true,editor:{type : 'textbox', options: {buttonText:'选择', onClickButton: openSelectEvalContent}}" >赋值内容</th>
                    <th data-options="field:'evalDesc',resizable:true,editor:'textbox'" >赋值描述</th>
                    <th data-options="field:'ioType',resizable:true" formatter="formatter_IO_TYPE" hidden="true">输入输出类型</th>
                    <th data-options="field:'mustNeed',resizable:true" formatter="formatter_STD_PUB_YN">是否必需</th>
                </tr>
            </thead>
        </table>
    </div>
    
    <div data-options="region:'south',split:false,collapsible:false"  style="width:100%;height:10%;">
        <div style="text-align: center; padding: 15px;height:100%;">
            <a href="javascript:void(0)" class="easyui-linkbutton"
                onclick="submitForm('In')">&nbsp;保&nbsp;存&nbsp;</a>
            <a href="javascript:void(0)" class="easyui-linkbutton"
                onclick="closeForm()">&nbsp;关&nbsp;闭&nbsp;</a>
        </div>
    </div>
    
    <script>
        var appId = $.getUrlParam('appId');
        var appModelId = $.getUrlParam('appModelId');
        var nodeId = $.getUrlParam('nodeId') == "undefined" ? null : $.getUrlParam('nodeId');
        var readonly = $.getUrlParam('readonly');
        var nodeType = decodeURI($.getUrlParam('nodeType'));
        readonly = readonly == "true" ;
        
        $(function () {

            // 内部服务
            queryFormIn();
            
        });

        function submitForm(dg) {
            if (endEditing($('#dgIn'))) {
                $.messager.alert('提示', '保存成功', 'info');
                //closeForm();
            }
        };

        function closeForm() {
            window.parent.JLEUtil.closewin();
        }

        function queryFormIn() {
            if (!nodeId) {
                return;
            }
            JLEUtil.queryForm('searchFormIn', 'dgIn',
                `../../api/osrv/isrv/evals?nodeId=${nodeId}&ioType=I`);
        }

        function queryFormOut() {
            if (!nodeId) {
                return;
            }
            JLEUtil.queryForm('searchFormOut', 'dgOut',
                `../../api/osrv/isrv/evals?nodeId=${nodeId}&ioType=O`);
        }

        function onInSelect(index, row ) {
            onSelect('In', index, row);
        }

        function onOutSelect(index, row) {
            onSelect('Out', index, row);
        }

        function onSelect(dgName, index, row) {
            $('#ff' + dgName).form('load', {
                recordKeyid : row.recordKeyid,
                nodeId : row.nodeId,
                inFieldCode : row.inFieldCode,
                inFieldName : row.inFieldName,
                evalSource : row.evalSource,
                evalContent : row.evalContent,
                evalDesc : row.evalDesc,
                ioType : row.ioType,
                mustNeed : row.mustNeed
            });	
        }

        function evalSourceChange(newValue) {
            setTimeout(()=> {
                var evalContentEd = $('#dgIn').datagrid('getEditor', { index: editIndex, field: 'evalContent' });
                if (newValue == "01") {
                    $(evalContentEd.target).textbox('readonly', false);
                } else {
                    $(evalContentEd.target).textbox('readonly', true);
                    
                }
            }, 50)
        }

        function openSelectEvalContent() {
            // var evalSource = $('#evalSource').combobox('getValue');
            var ed = $('#dgIn').datagrid('getEditor', { index: editIndex, field: 'evalSource' });
            var evalSource = $(ed.target).combobox('getValue');
            console.log(evalSource);
            if(evalSource == "02") {
                // 外部服务
                // JLEUtil.createModalWin('选择来自外服输入参数', `popOsrvInFieldChoice.html?appId=${appId}&appModelId=${appModelId}`);
                JLEUtil.createModalWin('选择来自外服输入参数', `popIsrvInoutInfoList.html?appId=${appId}&appModelId=${appModelId}&ioType=I&returnFunc=setPopIsrvOutFieldChoice`);
            } else if (evalSource == "03") {
                // 内部服务
                // JLEUtil.createModalWin('选择来自内服输出参数', `popIsrvOutFieldChoice.html?appId=${appId}&appModelId=${nodeRefModelId}`);
                JLEUtil.createModalWin('选择来自内服输出参数', `popIsrvOutFieldChoice.html?appId=${appId}&appModelId=${appModelId}&ioType=I&returnFunc=setPopIsrvOutFieldChoice`);
            } else {
                // 常量不处理
                return;
            }

        }

        function setPopOsrvInFieldChoice(selection) {
            if (selection) {
                var evalContentEd = $('#dgIn').datagrid('getEditor', { index: editIndex, field: 'evalContent' });
                var evalDescEd = $('#dgIn').datagrid('getEditor', { index: editIndex, field: 'evalDesc' });
                $(evalContentEd.target).textbox('setValue', selection.paramFieldCode);
                $(evalDescEd.target).textbox('setValue', selection.paramFieldName);
            }
        }

        function setPopIsrvOutFieldChoice(selection) {
            if (selection) {
                var evalContentEd = $('#dgIn').datagrid('getEditor', { index: editIndex, field: 'evalContent' });
                var evalDescEd = $('#dgIn').datagrid('getEditor', { index: editIndex, field: 'evalDesc' });
                $(evalContentEd.target).textbox('setValue', selection.paramFieldCode);
                $(evalDescEd.target).textbox('setValue', selection.paramFieldName);
            }
        }

        function onClickCell(index, field) {
            var dg = $('#dgIn');
            if (editIndex != index && !readonly) {
                console.log(editIndex, index)
                if (endEditing(dg)) {
                    dg.datagrid('selectRow', index).datagrid('beginEdit', index);
                    var ed = dg.datagrid('getEditor', {
                        index: index,
                        field: field
                    });
                    if (ed) {
                        ($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
                    }
                    editIndex = index;
                   
                } else {
                    setTimeout(function () {
                        dg.datagrid('selectRow', editIndex);
                    }, 0);
                }
            }
        }
        var editIndex = undefined;

        function endEditing(dg) {
            if (editIndex == undefined) {
                return true
            }
            if (dg.datagrid('validateRow', editIndex)) {
                dg.datagrid('endEdit', editIndex);
                editIndex = undefined;
                return true;
            } else {
                return false;
            }
        }

        function onEndEdit(index, field) {
            var selection = $('#dgIn').datagrid('getRows')[index];
            $.restfulPut('../../api/osrv/isrv/eval', selection, function (data) {
                //$('#dgIn').datagrid('reload');
                
            });
        }
    </script>
</body>

</html>